<?php

namespace app\common\controller;


trait SiteApp
{
    use PublicApp;


    public function index()
    {
        return $this->fetch();
    }


    public function add()
    {
        return $this->fetch();
    }


    public function edit()
    {
        $m  = new $this->model();
        $id = input('id');
        $s  = $m->getById($id);
        $this->assign('id', $id);
        $this->assign('s', $s);

        return $this->fetch();
    }


    public function detail()
    {
        $m  = new $this->model();
        $id = input('id');
        $s  = $m->getById($id);
        $this->assign('id', $id);
        $this->assign('s', $s);

        return $this->fetch();
    }


    public function del_submit()
    {
        $m  = new $this->model();
        $id = input('id');
        $s  = $m->remove($id);
        return js_return($s[0], $s[1]);
    }


    public function contents_submit()
    {
        $m   = new $this->model();
        $id  = input('id');
        $opt = input('post.');
        if ($id) {
            $s = $m->modify($opt, $id);
        }
        else {
            $s = $m->add($opt);
        }
        return js_return($s[0], $s[1]);
    }


    public function get_list($callback = '')
    {
        $m     = new $this->model();
        $types = $m->gets();
        $count = $m->getsCount();
        $lists = ['status' => 1, 'code' => 0, 'msg' => '', 'count' => $count, 'data' => []];
        foreach ($types as $key => &$val) {
            $callback && $callback($val);
            $lists['data'][] = $val;
        }
        return $lists;
    }

}
